﻿using EnvDTE80;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using EBFHelper.Nodes;
using EBFHelper.EBFHelperUtil;
using EBFHelper.forms;

using System.Xml;
using System.Diagnostics;
using System.Threading;


namespace EBFHelper
{
    public partial class EBFHelperForm : Form
    {
        private DTE2 _applicationObject;

        private List<iNode> actionXmlList;
        private List<iNode> operationXmlList;
        private List<iNode> opStepXmlList;
        private List<iNode> commConfigXmlList = new List<iNode>();
 
        private List<iNode> activeActionXmlList;
        private AutoCompleteStringCollection strings = new AutoCompleteStringCollection();

        private CommConfigInfoForm cCIF;

        public DTE2 applicationObject
        {
            get { return _applicationObject; }
            set { this._applicationObject = value; }
        }

        public EBFHelperForm(DTE2 applicationObject)
        {
            this._applicationObject = applicationObject;
            InitializeComponent();
            
        }

        private int checkconfigourationInfo()
        {
            try
            {
                //获取解决方案的所有项目
                System.Array theProjects = (System.Array)_applicationObject.ActiveSolutionProjects;

                if (theProjects.Length > 0)
                {
                    //遍历解决方案中的每一个项目
                    foreach (EnvDTE.Project theProject in theProjects)
                    {
                        if (!theProject.Name.Contains("WebSite"))
                        {
                            continue;
                        }
                        List<EnvDTE.ProjectItem> configourations = new List<EnvDTE.ProjectItem>();
                        //遍历项目中的每一个元素
                        HelperDisplayUtil.checkProjectItemNode(theProject.ProjectItems, configourations);
                        //获取到actions operations opsteps节点
                        if (configourations.Count > 0)
                        {
                            List<iNode> actionNodes = new List<iNode>();
                            List<iNode> operationNodes = new List<iNode>();
                            List<iNode> opStepNodes = new List<iNode>();

                            HelperDisplayUtil.findActionsAndOperationsAndOpstepsList(configourations, actionNodes, operationNodes,opStepNodes,commConfigXmlList);

                            actionXmlList = HelperDisplayUtil.findXmlNodeList(actionNodes);
                            operationXmlList = HelperDisplayUtil.findXmlNodeList(operationNodes);
                            opStepXmlList = HelperDisplayUtil.findXmlNodeList(opStepNodes);
                             
                            HelperDisplayUtil.bindSourceToTextBox(ActionListBox, actionXmlList);

                            HelperDisplayUtil.addAutoCompleteStringCollectionFromXmlNodeList(actionXmlList, strings);

                            SearchText.AutoCompleteCustomSource = strings;
                            SearchText.AutoCompleteSource = AutoCompleteSource.CustomSource;
                            SearchText.AutoCompleteMode = AutoCompleteMode.Suggest;
                            
                        }
                    }
                }

                return 0;
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message.ToString());
                return 1;
            }
        }

        private void ActionListBox_MouseClick(object sender, MouseEventArgs e)
        {
            int index = ActionListBox.IndexFromPoint(e.X, e.Y);
            ActionListBox.SelectedIndex = index;
            object item = ActionListBox.SelectedItem;
            ActionXmlNode actionXmlNode;
            if (null == (actionXmlNode = item as ActionXmlNode))
            {
                return;
            }
            //TODO
            HelperDisplayUtil.addOperationXmlNodeToActionXmlNode(actionXmlNode, operationXmlList);
            if (null == actionXmlNode.operationXmlNode)
            {
                if (OperationTreeView.Nodes.Count > 0)
                {
                    OperationTreeView.Nodes.Clear();
                }
                return;
            }
            MyTreeNode tNode = new MyTreeNode();
            tNode.name = actionXmlNode.operationXmlNode.projectItem.Name;
            tNode.Text = actionXmlNode.operationXmlNode.projectItem.Name;
            tNode.inode = actionXmlNode.operationXmlNode;

            HelperDisplayUtil.operationDataNodes.Clear();
            HelperDisplayUtil.operationAvailableFmt.Clear();
            HelperDisplayUtil.requestFmtNode.Clear();
            HelperDisplayUtil.buildTree(tNode.inode.xmlDoc.ChildNodes, tNode.Nodes,tNode.inode);
            foreach(MyTreeNode fmtNode in HelperDisplayUtil.requestFmtNode)
            {
                string[] str = fmtNode.Text.Split(new char[] { ':', '=', ';' });
                string refId = null;
                for (int i = 0; i < str.Length;i++ )
                {
                    if (str[i].Equals("refId"))
                    {
                        refId = str[i + 1];
                        break;
                    }
                    
                }
                if (HelperDisplayUtil.commAvailableFmt.Contains(refId) || HelperDisplayUtil.operationAvailableFmt.Contains(refId))
                {
                    continue;
                }
                else
                {
                    fmtNode.Text = "未定义的Fmt:" + fmtNode.Text;
                    fmtNode.ForeColor = Color.FromName("Red");
                }
            }

            OperationTreeView.Nodes.Clear();     
            OperationTreeView.Nodes.Add(tNode);
            OperationTreeView.ExpandAll();
            
        }

        private void ActionListBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = ActionListBox.IndexFromPoint(e.X, e.Y);
            ActionListBox.SelectedIndex = index;
            object item = ActionListBox.SelectedItem;
            ActionXmlNode actionXmlNode;
            if (null == (actionXmlNode=item as ActionXmlNode))
            {
                return;
            }
            ActionXmlNodeDetailInfo aXNDI = new ActionXmlNodeDetailInfo(actionXmlNode.xmlDoc);
            aXNDI.Show();
        }

        private void OperationTreeView_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            MyTreeNode tNode;
            if (null == (tNode=OperationTreeView.SelectedNode as MyTreeNode))
            {
                return;
            }
            
            XmlNodeAtrributeInfo xNAI = new XmlNodeAtrributeInfo(tNode,opStepXmlList);
            xNAI.Show();
        }

        private void EBFHelperForm_Load(object sender, EventArgs e)
        {
            checkconfigourationInfo();
            cCIF = new CommConfigInfoForm(commConfigXmlList);
        }

        private void SearchText_TextChanged(object sender, EventArgs e)
        {   
            if (SearchText.Text.Equals(""))
            {
                HelperDisplayUtil.bindSourceToTextBox(ActionListBox, actionXmlList);
            }
            else if(SearchText.Text.EndsWith(".xml"))
            {
                foreach(iNode inode in actionXmlList)
                {
                    ActionXmlNode actionXmlNode;
                    if (null == (actionXmlNode=inode as ActionXmlNode))
                    {
                        return;
                    }
                    
                    if(SearchText.Text.Equals(actionXmlNode.name))
                    {
                        HelperDisplayUtil.bindSourceToTextBox(ActionListBox, actionXmlNode);
                        break;
                    }
                }
            }
            else
            {
                return;
            }
        }

        private void ShowCommConfigInfoBtn_Click(object sender, EventArgs e)
        {
            if (cCIF.IsDisposed)
            {
                cCIF = new CommConfigInfoForm(commConfigXmlList);
            }
            
            cCIF.Show();
        }

        private void OperationTreeView_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (MouseButtons.Right != e.Button) return;
            if (null == e.Button) return;
            OperationTreeView.SelectedNode = e.Node;
            HelperModifyUtil.initContextMenuStrip();
            HelperModifyUtil.showContextMenuStrip(OperationTreeView,e.X,e.Y,(MyTreeNode)e.Node,opStepXmlList);
        }

        private void ActionListBox_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                int index = ActionListBox.IndexFromPoint(e.X, e.Y);
                ActionListBox.SelectedIndex = index;
                object item = ActionListBox.SelectedItem;
                ActionXmlNode actionXmlNode;
                if (null == (actionXmlNode = item as ActionXmlNode))
                {
                    return;
                }
                HelperModifyUtil.initContextMenuStrip();
                HelperModifyUtil.showContextMenuStrip(ActionListBox, e.X, e.Y, actionXmlNode.xmlDoc);
                
            }
           
        }

      
        
       
    }
}
